package com.newlink.building.common_base.utils

import android.app.ProgressDialog
import android.content.Context
import android.content.DialogInterface
import android.text.TextUtils
import androidx.appcompat.app.AlertDialog
import com.newlink.building.common_base.R
import com.newlink.building.common_base.widget.WaitProgressDialog

object DialogUtil {

    /**
     * 获取一个Dialog
     *
     * @param context
     * @return
     */
    fun getDialog(context: Context): AlertDialog.Builder {
        return AlertDialog.Builder(context)
    }

    /**
     * 获取一个耗时的对话框 ProgressDialog
     * 设计师要求目前所有加载状态不添加文字提示
     *
     * @param context
     * @return
     */
    fun getWaitDialog(context: Context): ProgressDialog {
        return WaitProgressDialog(context)
    }

    /**
     * 获取一个带文字的耗时对话框 ProgressDialog
     *
     * @param context
     * @param message
     * @return
     */
    fun getWaitDialog(context: Context, message: String): ProgressDialog {
        val waitDialog = WaitProgressDialog(context)
        if (!TextUtils.isEmpty(message)) {
            waitDialog.setMessage(message)
        }
        return waitDialog
    }

    fun getWaitDialog(context: Context, strRes: Int): ProgressDialog {
        return getWaitDialog(context, context.resources.getString(strRes))
    }

    /**
     * 获取一个信息对话框,注意需要自己手动调用show方法
     *
     * @param context
     * @param message
     * @param onClickListener
     * @return
     */
    @JvmOverloads
    fun getMessageDialog(
        context: Context, message: String,
        onClickListener: DialogInterface.OnClickListener? = null
    ): AlertDialog.Builder {
        val builder = getDialog(context)
        builder.setMessage(message)
        builder.setPositiveButton(
            context.getString(R.string.confirm_dialog_custom),
            onClickListener
        )
        return builder
    }

    fun getConfirmDialog(
        context: Context, message: String,
        onClickListener: DialogInterface.OnClickListener
    ): AlertDialog.Builder {
        val builder = getDialog(context)
        builder.setMessage(message)
        builder.setPositiveButton(
            context.getString(R.string.confirm_dialog_custom),
            onClickListener
        )
        builder.setNegativeButton(context.getString(R.string.cancel_dialog_custom), null)
        return builder
    }

    fun getMessageNewDialog(
        context: Context, message: String,
        button1: String,
        onClickListener1: DialogInterface.OnClickListener,
        button2: String,
        onClickListener2: DialogInterface.OnClickListener,
    ): AlertDialog.Builder {
        val builder = getDialog(context)
        builder.setMessage(message)
        builder.setPositiveButton(button1, onClickListener1)
        builder.setNegativeButton(button2, onClickListener2)
        return builder
    }

    fun getConfirmDialog(
        context: Context,
        message: String,
        onOKClickListener: DialogInterface.OnClickListener,
        onCancelClickListener: DialogInterface.OnClickListener
    ): AlertDialog.Builder {
        val builder = getDialog(context)
        builder.setMessage(message)
        builder.setPositiveButton(
            context.getString(R.string.confirm_dialog_custom),
            onOKClickListener
        )
        builder.setNegativeButton(
            context.getString(R.string.cancel_dialog_custom),
            onCancelClickListener
        )
        return builder
    }

    fun getSelectDialog(
        context: Context, title: String, arrays: Array<String>,
        onClickListener: DialogInterface.OnClickListener
    ): AlertDialog.Builder {
        val builder = getDialog(context)
        builder.setItems(arrays, onClickListener)
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title)
        }
        builder.setNegativeButton(context.getString(R.string.cancel_dialog_custom), null)
        return builder
    }

    fun getSelectDialog(
        context: Context, arrays: Array<String>,
        onClickListener: DialogInterface.OnClickListener
    ): AlertDialog.Builder {
        return getSelectDialog(context, "", arrays, onClickListener)
    }

    /**
     * 获取一个单选的对话框
     *
     * @param context
     * @param title
     * @param arrays
     * @param selectIndex
     * @param onClickListener
     * @param onOKClickListener
     * 点击确定的回调接口
     * @param onCancelClickListener
     * 点击取消的回调接口
     * @return
     */
    fun getSingleChoiceDialog(
        context: Context,
        title: String,
        arrays: Array<String>,
        selectIndex: Int,
        onClickListener: DialogInterface.OnClickListener,
        onOKClickListener: DialogInterface.OnClickListener,
        onCancelClickListener: DialogInterface.OnClickListener? = null
    ): AlertDialog.Builder {
        val builder = getDialog(context)
        builder.setSingleChoiceItems(arrays, selectIndex, onClickListener)
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title)
        }
        builder.setPositiveButton(
            context.getString(R.string.confirm_dialog_custom),
            onOKClickListener
        )
        builder.setNegativeButton(
            context.getString(R.string.cancel_dialog_custom),
            onCancelClickListener
        )
        return builder
    }

    fun getSingleChoiceDialog(
        context: Context, title: String, arrays: Array<String>,
        selectIndex: Int, onClickListener: DialogInterface.OnClickListener
    ): AlertDialog.Builder {
        val builder = getDialog(context)
        builder.setSingleChoiceItems(arrays, selectIndex, onClickListener)
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title)
        }
        builder.setPositiveButton(context.getString(R.string.cancel_dialog_custom), null)
        return builder
    }

    fun getSingleChoiceDialog(
        context: Context,
        arrays: Array<String>,
        selectIndex: Int,
        onClickListener: DialogInterface.OnClickListener,
        onOKClickListener: DialogInterface.OnClickListener,
        onCancelClickListener: DialogInterface.OnClickListener
    ): AlertDialog.Builder {
        return getSingleChoiceDialog(
            context, "", arrays, selectIndex, onClickListener, onOKClickListener,
            onCancelClickListener
        )
    }

    /**
     * 获取一个多选的对话框
     *
     * @param context
     * @param title
     * @param arrays
     * @param checkedItems
     * @param onMultiChoiceClickListener
     * @param onOKClickListener
     * 点击确定的回调接口
     * @param onCancleListener
     * 点击取消的回调接口
     * @return
     */
    fun getMultiChoiceDialog(
        context: Context,
        title: String,
        arrays: Array<String>,
        checkedItems: BooleanArray,
        onMultiChoiceClickListener: DialogInterface.OnMultiChoiceClickListener,
        onOKClickListener: DialogInterface.OnClickListener,
        onCancelClickListener: DialogInterface.OnClickListener
    ): AlertDialog.Builder {
        val builder = getDialog(context)
        builder.setMultiChoiceItems(arrays, checkedItems, onMultiChoiceClickListener)
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title)
        }
        builder.setPositiveButton(
            context.getString(R.string.confirm_dialog_custom),
            onOKClickListener
        )
        builder.setNegativeButton(
            context.getString(R.string.cancel_dialog_custom),
            onCancelClickListener
        )
        return builder
    }

}

